package assetservicelogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
)

type AssetPurchaseRequestPageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAssetPurchaseRequestPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssetPurchaseRequestPageLogic {
	return &AssetPurchaseRequestPageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AssetPurchaseRequestPage 分页查询采购申请单表
func (l *AssetPurchaseRequestPageLogic) AssetPurchaseRequestPage(in *property_service.AssetPurchaseRequestPageReq) (*property_service.AssetPurchaseRequestPageRes, error) {
	// 定义一个采购申请单表模型对象切片用于存储查询结果
	var data []*model.AssetPurchaseRequest
	// 获取采购申请单表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).AssetPurchaseRequest
	// 构建查询条件：根据租户ID查询
	do := m.WithContext(l.ctx).Where(m.TenantID.Eq(in.Where.TenantID)) // 租户ID

	// 动态构建查询条件
	if in.Where.ID > 0 {
		do = do.Where(m.ID.Eq(in.Where.ID)) // ID
	} else {
		// 处理其他字段
		if in.Where.RequestCode != "" {
			do = do.Where(m.RequestCode.Like("%" + in.Where.RequestCode + "%")) // 申请单号
		}
		if in.Where.PurchaseMonth != "" {
			do = do.Where(m.PurchaseMonth.Eq(BeanUtils.GetDateFormat(in.Where.PurchaseMonth))) // 购置年月
		}
		if in.Where.RequestTypeID != "" {
			do = do.Where(m.RequestTypeID.Eq(in.Where.RequestTypeID)) // 申请类型ID
		}
		if in.Where.RequestType != "" {
			do = do.Where(m.RequestType.Like("%" + in.Where.RequestType + "%")) // 申请类型
		}
		if in.Where.PlanNumber != "" {
			do = do.Where(m.PlanNumber.Like("%" + in.Where.PlanNumber + "%")) // 计划号
		}
		if in.Where.RequestDept != "" {
			do = do.Where(m.RequestDept.Like("%" + in.Where.RequestDept + "%")) // 申请科室
		}
		if in.Where.RequestDeptID > 0 {
			do = do.Where(m.RequestDeptID.Eq(in.Where.RequestDeptID)) // 申请科室id
		}
		if in.Where.ApplicantID > 0 {
			do = do.Where(m.ApplicantID.Eq(in.Where.ApplicantID)) // 申请人id
		}

		if in.Where.Applicant != "" {
			do = do.Where(m.Applicant.Like("%" + in.Where.Applicant + "%")) // 申请人
		}
		if in.Where.CompileDate != "" {
			do = do.Where(m.CompileDate.Eq(BeanUtils.GetDateFormat(in.Where.CompileDate))) // 编制日期
		}
		if in.Where.Remarks != "" {
			do = do.Where(m.Remarks.Like("%" + in.Where.Remarks + "%")) // 备注
		}
		if in.Where.PurchaseStatus != "" {
			do = do.Where(m.PurchaseStatus.Like("%" + in.Where.PurchaseStatus + "%")) // 采购状态
		}
		if in.Where.StockID > 0 {
			do = do.Where(m.StockID.Eq(in.Where.StockID)) // 仓库ID
		}
		/*		if in.Where.StatusID >= 0 {
				do = do.Where(m.StatusID.Eq(in.Where.StatusID)) // 仓库ID
			}*/
	}

	// 定义一个变量用于存储总记录数
	var count int64

	do = do.Order(m.UpdatedAt.Desc())
	// 判断是否需要分页
	if in.Page+in.Limit <= 1 {
		// 如果不需要分页，查询所有记录
		data, _ = do.Find()
		// 设置总记录数为查询结果的长度
		count = int64(len(data))
	} else {
		// 如果需要分页，查询指定页的数据和总记录数
		data, count, _ = do.FindByPage(int((in.Page-1)*in.Limit), int(in.Limit))
	}

	// 将查询结果转换为响应对象
	list, _ := BeanUtils.CopyProperties(data, property_service.AssetPurchaseRequest{})

	// 返回查询结果，包括总记录数和数据列表
	return &property_service.AssetPurchaseRequestPageRes{
		Count: count,
		List:  list.([]*property_service.AssetPurchaseRequest),
	}, nil
}
